package com.zj.osm.model.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zj.osm.model.pojo.EquipType;
import com.zj.osm.utils.SQLiteDatabaseUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @author jiang zhu on 2019/10/25
 */
public class EquipTypeDao {

    private static final String SELECT_FROM_EQUIP_TYPE = "select * from EquipType";
    private static final String SELECT_FROM_EQUIP_TYPE_WHERE_COMPONENT = "select * from EquipType where component=?";
    private SQLiteDatabase sqLiteDatabase = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
    /**
     * 获取EquipType集合
     *
     * @return /
     */
    public List<EquipType> getEquipTypeList() {
        //LinkedList增删快  ArrayList查询快
        List<EquipType> equipTypeList = new LinkedList<>();
        Cursor cursor = sqLiteDatabase.rawQuery(SELECT_FROM_EQUIP_TYPE, null);
        while (cursor.moveToNext()) {
            EquipType equip = new EquipType(
                    cursor.getInt(0),
                    cursor.getString(1),
                    cursor.getInt(2),
                    cursor.getString(3),
                    cursor.getInt(4),
                    cursor.getString(5),
                    cursor.getString(6)
            );
            equipTypeList.add(equip);
        }
        cursor.close();
        return equipTypeList;
    }

    /**
     * 通过component值来查询对应的EquipType
     *
     * @param component /
     * @return /
     */
    public List<EquipType> getEquipTypeListByComponent(int component) {
        //LinkedList增删快  ArrayList查询快
        List<EquipType> equipTypeList = new LinkedList<>();
        Log.e("TAG", "getEquipTypeList: " + sqLiteDatabase);
        Cursor cursor = sqLiteDatabase.rawQuery(SELECT_FROM_EQUIP_TYPE_WHERE_COMPONENT, new String[]{String.valueOf(component)});
        while (cursor.moveToNext()) {
            EquipType equip = new EquipType(
                    cursor.getInt(0),
                    cursor.getString(1),
                    cursor.getInt(2),
                    cursor.getString(3),
                    cursor.getInt(4),
                    cursor.getString(5),
                    cursor.getString(6)
            );
            equipTypeList.add(equip);
        }
        cursor.close();
        return equipTypeList;
    }

}
